<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传文件</title>

    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

    <script>
        $(function () {
            $("#comment").click(function () {
                var formData = new FormData();
                formData.append("file",$("#file")[0].files[0]);
                //文件类型校验
                var flie = $("#file").val();
                var fileSuffix = flie.split(".")[1];
                if(fileSuffix != "xlsx" && (fileSuffix != "xls")){
                    swal("文件类型解析错误，只能导入excel格式文件（以.xlsx或.xls结尾的文件）");
                    return;
                }
                $.ajax({
                    type:"post",
                    url:"/user/comment",
                    data:formData,
                    dataType:"json",
                    cache:false,
                    contentType:false,
                    processData:false,
                    mimeType:"multipart/form-data",  //必须设置多媒体文件的方式上传
                    success:function(data){
                        alert("添加成功");
                        window.location.href = "/list";
                    },
                    error:function(){
                        alert("添加失败");
                    },
                });
            })
        })

    </script>
</head>
<body>
<div>
    <form enctype="multipart/form-data">
        <div >
            <label >
                <span >*</span>  选择文件：
            </label>
            <div >
                <input type="file" id="file" name="file" />
            </div>
        </div>
        <div>
            <input type="button" value="上传" id ="comment" >
        </div>
    </form>
</div>

</body>

</html>